package com.zztest1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) {
		System.out.println("Hello World!");
		User user1 = createUser(1004L, "zhangsan", 55);
		User user2 = createUser(1007L, "lisi", 28);
		User user3 = createUser(1001L, "wangwu", 19);
		User user4 = createUser(1005L, "zhaoliu", 17);
		User user5 = createUser(1009L, "chenqi", null);
		User user6 = createUser(1002L, null, 37);
		User user7 = createUser(1008L, "bajie", 8);
		User user8 = createUser(1006L, "saseng", 26);
		User user9 = createUser(1003L, "bailongma", 13);
		List<User> list = new ArrayList<>();
		list.add(user1);
		list.add(user2);
		list.add(user3);
		list.add(user4);
		list.add(user5);
		list.add(null);
		list.add(user6);
		list.add(user7);
		list.add(user8);
		list.add(user9);
		Comparator<User> fun = new Comparator<User>() {

			@Override
			public int compare(User o1, User o2) {
				String name1 = o1.getName();
				String name2 = o2.getName();
				if (name1 == null) {
					return 1;
				}
				if (name2 == null) {
					return -1;
				}
				return name1.compareTo(name2);
			}

		};
		Collections.sort(list, Comparator.nullsLast(fun));
//		Collections.sort(list, Comparator.nullsLast(Comparator.comparing(User::getName)));
		for (int i = 0; i < list.size(); i++) {
			User user = list.get(i);
			System.out.println(user);
		}
	}

	private static User createUser(Long id, String name, Integer age) {
		User user = new User();
		user.setId(id);
		user.setName(name);
		user.setAge(age);
		return user;
	}
}
